package com.jodinfo.outmirror.interceptor;

import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class SystemSessionInterceptor implements HandlerInterceptor {
	
	//private List excludedUrls;
	
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		 	String url = request.getServletPath().toString();
		 	String contextPath = request.getContextPath();
		 	HttpSession session=request.getSession(true);  
	        Object obj = session.getAttribute("user");  
	        if (obj==null||"".equals(obj.toString())) {
	        	response.sendRedirect((new StringBuilder()).append(contextPath).append("/officialMain/login?redirectURL=").append(URLEncoder.encode(url)).toString());
	        	//response.sendRedirect((new StringBuilder()).append(contextPath).append("/officialMain/Login?redirectURL=").append(URLEncoder.encode(url)).toString());
	                 return false;
	           }
	           return true;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub

	}

}
